package com.qidian.by.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qidian.by.goods.dto.ItemInfoDTO;
import com.qidian.by.goods.mapper.ItemDescMapper;
import com.qidian.by.goods.mapper.ItemImageMapper;
import com.qidian.by.goods.mapper.ItemMapper;
import com.qidian.by.goods.mapper.ItemSpecificationMapper;
import com.qidian.by.goods.service.IItemService;
import com.qidian.by.goods.service.RabbitTemplateClientService;
import com.qidian.by.goods.service.feign.EsSearchServiceClient;
import com.qidian.by.goods.vo.ItemVO;
import com.qidian.by.pojo.goods.entity.Item;
import com.qidian.by.pojo.goods.entity.ItemDesc;
import com.qidian.by.pojo.goods.entity.ItemImage;
import com.qidian.by.util.exceptions.ServiceExceptionEnum;
import com.qidian.by.util.pojo.EsResult;
import net.sf.jsqlparser.parser.Provider;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.annotation.Resources;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品表 服务实现类
 * </p>
 *
 * @author 小可怎么都睡不够
 * @since 2024-05-08
 */
@Service
@Transactional
public class ItemServiceImpl implements IItemService {
    @Resource
    private ItemMapper itemMapper;
    @Resource
    private ItemImageMapper itemImageMapper;
    @Resource
    private ItemDescMapper itemDescMapper;
    @Resource
    private ItemSpecificationMapper itemSpecificationMapper;




    @Resource
    RabbitTemplateClientService itemRabbitTemplateClientService;
    @Resource
    RabbitTemplateClientService searchRabbitTemplateClientService;

    @Override
    public EsResult save(ItemVO vo) {
        vo.setOption("INSERT");
        // 保存商品信息
        int insert = itemMapper.insert(vo);
        if (insert == 1) {
            // 保存商品成功之后，就有商品编号了。
            Long itemId = vo.getItemId();
            // 给图片设置商品编号
            List<String> imageUrls = vo.getImageUrl();
            // 保存所有的图片
            for (String imageUrl : imageUrls) {
                // 保存图片
                ItemImage itemImage = new ItemImage();
                itemImage.setItemId(itemId);
                itemImage.setImageUrl(imageUrl);
                itemImageMapper.insert(itemImage);
            }
            // 保存商品详情信息
            ItemDesc itemDesc = new ItemDesc();
            itemDesc.setItemId(itemId);
            itemDesc.setItemDesc(vo.getItemDesc());
            itemDescMapper.insert(itemDesc);
//            esSearchServiceClient.insert(EsResult.object2json(vo));
            searchRabbitTemplateClientService.send(EsResult.object2json(vo));
            itemRabbitTemplateClientService.send(EsResult.object2json(getItemInfoById(itemId)));
            return EsResult.ok();
        }
        return EsResult.error(ServiceExceptionEnum.PARAM_ERROR);
    }

    @Override
    public EsResult list(Item item, int page, int limit) {
        IPage<Item> iPage = new Page<>(page, limit);
        QueryWrapper<Item> wrapper = new QueryWrapper<>();
        // 查询条件
        if (item != null) {
            wrapper.like(!StringUtils.isBlank(item.getItemTitle()), "item_title", item.getItemTitle());
            wrapper.eq(item.getCatId() != null && item.getCatId() != 0, "cat_id", item.getCatId());
            wrapper.eq(item.getStatus() != null && item.getStatus() != 0, "status", item.getStatus());
            wrapper.between(item.getCreateStart() != null && item.getCreateEnd() != null, "created", item.getCreateStart(), item.getCreateEnd());
        }
        return EsResult.ok("查询成功", itemMapper.selectPage(iPage, wrapper));
    }

    @Override
    public EsResult addItemAndSpeRelation(Long itemId, List<Long> speId) {
        // 删除之前的关系
        itemMapper.deleteItemAndSpeRelationByItemId(itemId);
        // 增加新的关系
        itemMapper.addItemAndSpeRelation(itemId, speId);
        return EsResult.ok();
    }

    @Override
    public EsResult getSpeIdByItemId(Long itemId) {
        return EsResult.ok("查询成功", itemMapper.getSpeIdByItemId(itemId));
    }

    @Override
    public EsResult getItemInfoById(Long itemId) {
        ItemInfoDTO infoDTO = new ItemInfoDTO()
                .setItem(itemMapper.selectById(itemId))
                .setItemDesc(itemDescMapper.selectById(itemId).getItemDesc())
                .setItemSpe(()-> itemSpecificationMapper.getAllItemSpeInfoByItemId(itemId))
                .setItemImages(() -> {
                    QueryWrapper<ItemImage> wrapper = new QueryWrapper<>();
                    wrapper.eq("item_id", itemId);
                    return itemImageMapper.selectList(wrapper).stream()
                            .map(ItemImage::getImageUrl)
                            .collect(Collectors.toList());
                });
        return EsResult.ok("查询成功",infoDTO);
    }

    @Override
    public EsResult getItemById(Long itemId) {
        Item item = itemMapper.selectById(itemId);
        QueryWrapper<ItemImage> imageWrapper = new QueryWrapper<>();
        imageWrapper.eq("item_id",item.getItemId());
        List<ItemImage> itemImages = itemImageMapper.selectList(imageWrapper);
        List<String> images = new ArrayList<>();
        itemImages.forEach(image->{
            images.add(image.getImageUrl());
        });
        item.setImages(images);
        return EsResult.ok(item);
    }
}
